import os

import xlrd


class excel_data:
    """读取Excel表数据"""
    path = "/Users/lx/Desktop/xlsx"
    files = os.listdir(path)
    # print(files)
    file_files = []
    for file in files:
        if file.endswith('.xlsx'):
            # df = pd.read_excel(os.path.join(path, file))
            # 获取文件的前缀名称
            file_qian = file[0:len(file) - 5]
            file_files.append(file_qian)

    for i in range(len(file_files)):
        if file_files[i] == "ceshi":
            # global rows, cols
            # 打开Excel文件
            excel = xlrd.open_workbook(path + "/" + file_files[i] + ".xlsx")

            # 通过下标定位表格
            sheet = excel.sheet_by_index(0)

            # 获取行和列数
            rows, cols = sheet.nrows, sheet.ncols

    def read_excel(self):
        # 获取第一行数据key
        first_row = self.sheet.row_values(0)
        # 定义一个空列表
        self.result = []
        # 从第一行用例开始循环
        for i in range(1, self.rows):
            # 定义空列表
            self.info_dict = {}
            # 再次循环取出列
            for j in range(0, self.cols):
                # 添加到字典中
                self.info_dict[first_row[j]] = self.sheet.row_values(i)[j]
                # print(info_dict)
            # 将数据放入列表中
            self.result.append(self.info_dict)
        # print(self.result)

        # c = open("../csv_util/csv_file.csv", "w")
        # for i in self.result:
        #     c.write(",".join(i) + "\n")
        # c.close()


ex = excel_data()
ex.read_excel()

# if __name__ == "__main__":
#     ex = excel_data()
#     ex.read_excel()
